# -*- coding:utf-8 -*-
"""
作者：RyanLai
日期：2023.02.04
"""
import cv2 as cv

i = 0
a = "Mix" + str(i) + ".jpg"

device = cv.VideoCapture(0)  # change your device ID
device.set(3, 1920)
device.set(4, 1080)
device.set(cv.CAP_PROP_FPS, 60)
print(device.get(3), device.get(4), device.get(cv.CAP_PROP_FPS))

if device.isOpened():
    print("Camera opened success!")
    while (True):
        ret, frame = device.read()
        height, width, _ = frame.shape

        # # 计算裁剪的起始坐标
        x = (width - 640) // 2
        y = (height - 560) // 2

        # 进行中心裁剪
        frame = frame[y:y + 640, x:x + 640]
        cv.imshow("device_frame", frame)

        if cv.waitKey(1) & 0xFF == 113:  # push 'q' to save image
            ret1 = cv.imwrite(a, frame)
            if ret1:
                print("Saved Pic:" + a)
            else:
                print("Saved Failed")
            i += 1
            a = "Mix" + str(i) + ".jpg"
        elif cv.waitKey(1) & 0xFF == 27:  # push 'ESC' to exit
            cv.destroyAllWindows()
            print("Camera Shut Down!")
            device.release()
            break

else:
    print("Camera opened failed!")
